import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.V1_7;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;

public class JavaMethodOOM {

	public static void main(String[] args) throws IOException, ClassNotFoundException {

		int i = 0;
		try {
			while (true) {
				ClassWriter cw = new ClassWriter(0);
				cw.visit(V1_7, ACC_PUBLIC, "DeptBean" + (++i), null, "java/lang/Object", null);
				cw.visitField(ACC_PRIVATE, "name", "I", null, null).visitEnd();
				MethodVisitor mv;
				mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
				if (mv != null) {
					mv.visitCode();
					mv.visitVarInsn(ALOAD, 0);
					mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
					mv.visitInsn(RETURN);
					mv.visitMaxs(1, 1);
					mv.visitEnd();
				}
				String path = JavaMethodOOM.class.getResource(".").getPath();
				FileOutputStream fileOutputStream = new FileOutputStream(new File(path, "DeptBean" + i + ".class"));
				fileOutputStream.write(cw.toByteArray());
				fileOutputStream.flush();
				fileOutputStream.close();

				ClassLoader.getSystemClassLoader().loadClass("DeptBean" + i);
			}
		} catch (Throwable e) {
			//10M加载类: 7237
			//20M加载类: 17640
			System.out.println("20M加载类: " + i);
			throw e;
		}

	}

}
